<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>用户添加</title>
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script>

        layui.use(['form', 'jquery','element'], function () {
            var form = layui.form;
            var $ = layui.$;
            
            form.render();
            form.on('select(duty)', function (data) {
                var duty = $("#duty").val();
                var ruledata = selectRuleInfo();
                ruleOnload(ruledata);
                if (duty == 0) {
                    $("#cityId").attr("disabled", true);
                    $("#countyId").attr("disabled", true);
                    $('#checkbox').html("");
                    form.render();
                } else if (duty == 1){//地市
                    $("#cityId").attr("disabled", false);
                    $("#countyId").attr("disabled", true);
                    $('#checkbox').html("");
                    form.render();
                } else if (duty == 3){//区县
                    $("#cityId").attr("disabled", false);
                    $("#countyId").attr("disabled", false);
                    $('#checkbox').html("");
                    form.render();
                } else if(duty == 2){
                    $("#cityId").attr("disabled", false);
                    $("#countyId").attr("disabled", false);
                    form.render();
                } else if (duty == 4){//客户
                    $("#cityId").attr("disabled", false);
                    $("#countyId").attr("disabled", false);
                    $('#checkbox').html("");
                    form.render();
                } else if (duty == 5){//地市领导
                    $("#cityId").attr("disabled", false);
                    $("#countyId").attr("disabled", true);
                    $('#checkbox').html("");
                    form.render();
                }
            });
            //监听区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data3 = selectMgrInfo(data.value);
                appendMgr(data3);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="999">全部</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            
            //监听项目
            form.on('select(countyId)', function (data) {
                var data1 = selectMgrInfo(data.value);
                appendMgr(data1);
            });
           
            //根据城市ID和区县查询项目
            function selectMgrInfo(countyId) {
            	if(countyId == 999){
            		countyId = $("#cityId").val();
            	}
                var data;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            
            //动态拼接项目
            function appendMgr(data) {
               var duty = $("#duty").val();
               if(duty==2) {
                    if (data.length >0) {
                        var checkboxs = '';
                        for (var i = 0; i < data.length; i++) {
                            checkboxs += '<input type="checkbox" value="' + data[i].id + '" name="mgrId"  title="' + data[i].name + '" />';
                        }
                        $('#checkbox').html(checkboxs);
                    } else {
                        var checkboxs = '<span>该区域下没有项目</span>';
                        $('#checkbox').html(checkboxs);
                    }
                }
                form.render();
            }
            

            

            function selectRuleInfo() {
                var ruledata;
                var url = '/user/findRule';
                var param = {
                	duty : $("#duty").val()
                }
                
                $.ajax({
                        type: 'post',
                        async: false,
                        data:param,
                        url: url,
                        success: function (json) {
                            ruledata = json.data;
                        }
                    }
                );
                return ruledata;
            }

            function ruleOnload( ruledata) {
                var rulecheckboxs = "";
                for (var i = 0; i < ruledata.length; i++) {
                    rulecheckboxs += '<input type="checkbox"  value="' + ruledata[i].roleId + '" name="roleId" title="' + ruledata[i].roleName + '">';
                }
                $('#rulecheckbox').html(rulecheckboxs);
                form.render();
            }

            $('#add').on('click', function () {
                var url = '/user/add';
                var mgrId = "";
                var roleId = "";
                $("input:checkbox[name='mgrId']:checked").each(function () {
                    mgrId += $(this).val() + ",";
                });
                $("input:checkbox[name='roleId']:checked").each(function () {
                    roleId += $(this).val() + ",";
                });
                var userName = $.trim($("#username").val());
                var password =$.trim($("#password").val());
                var msisdn = $.trim($("#msisdn").val());
                var duty = $("#duty").val();
                var cityId = $("#cityId").val();
                var countyId = $("#countyId").val();
                
                if (isNull(userName)) {
                    layer.msg("请输入用户名");
                    $("#username").focus();
                } else if (testMsisdn(msisdn) || isNull(msisdn)) {
                    layer.msg("请输入合法的手机号码");
                    $("#msisdn").focus();
                } else if (testPassword(password) || isNull(password)) {
                    layer.msg("您输入的密码不符合规范");
                    $("#password").focus();
                } else if (duty == "") {
                    layer.msg("请选择用户级别");
                } else if (cityId == "" && duty > 0 && duty <4) {
                    layer.msg("请选择一个地市");
                }  else if (duty == 2 && mgrId == "") {
                    layer.msg("您至少需要选择一个项目");
                } else if(isNull(roleId)){
                    layer.msg("您至少需要选择一个权限");
                }else {
                    if (duty==0 ){
                        cityId="25";
                    }
                    var param = {
                        userName: userName,
                        msisdn: msisdn,
                        password: password,
                        duty: duty,
                        cityId: cityId,
                        countyId: countyId,
                        mgrId: mgrId,
                        roleId: roleId
                    }
                    $.ajax({
                        type: 'post',
                        data: param,
                        url: url,
                        beforeSend: function () {
                        },
                        success: function (json) {
                            if (json == 1) {
                                layui.use('layer', function () {
                                    layer.confirm('添加成功，是否返回', {icon: 3, title: '提示'}, function (index) {
                                        layer.close(index);
                                        location.href = "operatorManage.html";
                                    });
                                });
                            } else if (json == -1) {
                                layer.msg('您输入的手机号已经存在');
                            }
                            else {
                                layer.msg('信息有误');
                            }
                        },
                        complete: function () {

                        },
                        error: function (data) {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    });
                }
            });
        });
    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
  <a href="javascript:returnIndex2()">首页&nbsp;</a>
  <a href=""> &nbsp;系统管理&nbsp; </a>
  <a href="operatorManage.html"> &nbsp;用户管理&nbsp; </a>
 <a href="operatorAdd.html"> &nbsp;用户添加&nbsp; </a>
</span>
        </div>
        <div class="content-box">
            <div class="layui-form layui-form-pane" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户姓名</label>
                        <div class="layui-input-block">
                            <input type="text" id="username" name="username" lay-verify="required" placeholder="输入用户姓名"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号码</label>
                        <div class="layui-input-block">
                            <input type="text" id="msisdn" name="msisdn" lay-verify="required" placeholder="输入手机号码"
                                   autocomplete="off" class="layui-input" maxlength="11">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-inline">
                            <input type="text" id="password" name="password" lay-verify="required" placeholder="输入密码"
                                   autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline">
                            <p style="color: red;font-size:6px">提示:密码数量至少为8位，并且必须包含数字，字母和特殊的字符，不能是简单的数字连续密码</p>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">级别</label>
                        <div class="layui-input-block">
                            <select name="duty" lay-filter="duty" id="duty">
                                <option value=""></option>
                                <option value="0">省公司管理员</option>
                                <option value="1">地市管理员</option>
                                <option value="2">项目管理员</option>
                                <option value="3">区县管理员</option>
                                <option value="4">用户</option>
                                <option value="5">地市领导</option>
                                <option value="6">供电局领导</option>
                                <option value="7">供电局操作员</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">归属地</label>
                        <div class="layui-input-block">
                            <select name="interest" lay-filter="aihao" disabled>
                                <option value="1"> 江苏</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" id="city">
                        <label class="layui-form-label">地市</label>
                        <div class="layui-input-block">
                            <select name="cityId" lay-filter="cityId" id="cityId">
                                <option value="">全部</option>
                                <option value="11">苏州</option>
                                <option value="12">淮安</option>
                                <option value="13">宿迁</option>
                                <option value="14">南京</option>
                                <option value="15">连云港</option>
                                <option value="16">徐州</option>
                                <option value="17">常州</option>
                                <option value="18">镇江</option>
                                <option value="19">无锡</option>
                                <option value="20">南通</option>
                                <option value="21">泰州</option>
                                <option value="22">盐城</option>
                                <option value="23">扬州</option>
                                <option value="25">省级</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" id="city">
                        <label class="layui-form-label">区县</label>
                        <div class="layui-input-block">
                            <select name="countyId" lay-filter="countyId" id="countyId">
									<option value="999">全部</option>
								</select>
                        </div>
                    </div>
                    <br>
                    <br>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">项目</label>
                            <div class="layui-input-block" id="checkbox">
                            </div>
                        </div>
                    </div>
                </div>
            
                <div class="layui-form-item">
                    <div class="layui-inline" pane="">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block" id="rulecheckbox">

                        </div>
                    </div>
                </div>
                <div class="layui-block" pane="">
                    <button class="layui-btn layui-btn-normal" id="add">新&nbsp;增</button>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</body>
</html>